﻿using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {

    protected BulletData _bulletData = null;
    public BulletData bulletData {
        get {
            if (_bulletData == null) {
                _bulletData = new BulletData();
                _bulletData.Init();
            }
            return _bulletData;
        }
    }

    public bool isValid {
        get {
            return _bulletData == null ? false : _bulletData.isValid;
        }
        set {
            if (_bulletData != null) {
                _bulletData.isValid = value;
            }
        }
    }

    protected Collider _collider;
    void Start() {
        _collider = GetComponent<Collider>();
        _collider.isTrigger = true;
        isValid = true;
    }

    public void Tick() {
        if (isValid == false)
            return;

        this._bulletData.Tick();
        transform.position = _bulletData.position;
    }

    void OnTriggerEnter(Collider collider) {
        isValid = false;
    }

    public void BulletDestroy() {
        isValid = false;
        GameScene.instance.SpawnEffect("Effects/Explosions/Explosion_0", transform.parent, transform.position, 0.5f);
        GameObject.Destroy(gameObject);
    }

}
